﻿using Maintanence_Printing_Tool;
//using Maintanence_Printing_Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class ViewAllAttendance : Form
    {
        string sqlText;
        static string connection = ConfigurationManager.AppSettings["ConStr"].ToString();
        SqlConnection con = new SqlConnection(connection);
        public ViewAllAttendance()
        {
            InitializeComponent();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
          
            
        }
        
        private void ViewAllAttendance_Load(object sender, EventArgs e)
        {
            cmbMonths.SelectedText = "01";
            cmbYears.SelectedText = "2015";
           //monthYear();
           fetch();
        }

        private void monthYear()
        {
            var months = new Dictionary<string, string>();

            months["MM"] = "MM";
            months["January"] = "01";
            months["February"] = "02";
            months["March"] = "03";
            months["April"] = "04";
            months["May"] = "05";
            months["June"] = "06";
            months["July"] = "07";
            months["August"] = "08";
            months["September"] = "09";
            months["October"] = "10";
            months["Novenber"] = "11";
            months["December"] = "12";
            cmbMonths.DataSource = new BindingSource(months, null);
            cmbMonths.DisplayMember = "Value";
            cmbMonths.ValueMember = "Key";

            
        }
        private void fetch()
        {
            try
            {
                string fetchAttendance = "select a.id, m.firstname, m.lastname, a.indate, a.intime, a.outdate, a.outtime from Attendance as a inner join member as m on m.id = a.MemberId order by firstname";
                SqlCommand cmd = new SqlCommand(fetchAttendance, con);
                DataTable dt = new DataTable();
                SqlDataAdapter sqlAdaptor = new SqlDataAdapter(cmd);
                sqlAdaptor.Fill(dt);
                dataGridView1.DataSource = dt;

                if (dt.Rows.Count == 0)
                {
                    DialogResult result = MessageBox.Show("No Details Found", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        this.BeginInvoke(new MethodInvoker(Close));
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Attendance loading failed !!!...", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }

        private void buttonMonthWiseSearch_Click(object sender, EventArgs e)
        {
           
        }

        private void buttonSearch_Click_1(object sender, EventArgs e)
        {
            try
            {
                DateTime FromDt = DateTime.Parse(FromDatePicker.Text.ToString());
                string FromDt_st = FromDt.ToString("yyyyMMdd");

                DateTime ToDt = DateTime.Parse(ToDatePicker.Text.ToString());
                string ToDt_st = ToDt.ToString("yyyyMMdd");

                DataTable dtClassi = new DataTable();
                //sqlText = "select * from attendance where indate between '" + FromDt_st + "' and '" + ToDt_st + "'";
                sqlText = "select a.id, m.firstname, m.lastname, a.indate, a.intime, a.outdate, a.outtime from Attendance as a inner join member as m on m.id = a.MemberId where indate between '" + FromDt_st + "' and '" + ToDt_st + "' order by firstname";
                dtClassi = dbFunctions.getTable(sqlText);
                dataGridView1.DataSource = dtClassi;

                if (dtClassi.Rows.Count == 0)
                {
                    DialogResult ResultFromToDate = MessageBox.Show("No Details Found", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (ResultFromToDate == DialogResult.OK)
                    {
                        this.BeginInvoke(new MethodInvoker(Close));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Attendance loading failed !!!...", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void buttonMonthWiseSearch_Click_1(object sender, EventArgs e)
        {
            try
            {
                //if (cmbMonths.Text == "MM" || cmbYears.Text == "YYYY")
                //{
                //    MessageBox.Show("Please select valid Month and Year", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}

                DataTable dtMonthwiseReport = new DataTable();
                sqlText = "select a.id, m.firstname, m.lastname, a.indate, a.intime, a.outdate, a.outtime from Attendance as a inner join member as m on m.id = a.MemberId where MONTH(indate) = '" + cmbMonths.Text + "' and YEAR(indate)='" + cmbYears.Text + "' order by firstname";
                dtMonthwiseReport = dbFunctions.getTable(sqlText);
                dataGridView1.DataSource = dtMonthwiseReport;

                if (dtMonthwiseReport.Rows.Count == 0)
                {
                    DialogResult Result_MonthWise = MessageBox.Show("No Details found", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (Result_MonthWise == DialogResult.OK)
                    {
                        this.BeginInvoke(new MethodInvoker(Close));
                    }
                }
            }
            catch(Exception exc)
            {
                MessageBox.Show("Attendance loading failed !!!...", "Attendance Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }                
    }
}
